﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using StockholmsStad.Data.Interfaces;
using StockholmsStad.Runtime.Serialization.DataContract;

namespace StockholmsStad.Business.Test
{
    /// <summary>
    ///This is a test class for UserBusinessTest and is intended
    ///to contain all UserBusinessTest Unit Tests
    ///</summary>
    [TestClass()]
    public class UserBusinessTest
    {
        private IDataContext _context;
        private MockRepository _mocks = new MockRepository();
        private const User _user = null;

        #region Additional test attributes

        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}

        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void MyTestInitialize()
        {
            _mocks = new MockRepository();
        }

        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //

        #endregion

        private UserBusiness GetTarget(IAgentFactory agentFactory)
        {
            var target = new UserBusiness(_user);
            target.AgentFactory = agentFactory;
            _context = _mocks.DynamicMock<IDataContext>();
            IContextFactory contextFactory = new MockContextFactory(_context);
            //target.ContextFactory = contextFactory;
            return target;
        }

        /// <summary>
        ///A test for IsValidWebUser
        ///</summary>
        [TestMethod()]
        public void IsValidWebUserTest()
        {
            var agentFactory = new MockAgentFactory();
            var userAgent = _mocks.StrictMock<IUserAgent>();
            agentFactory.UserAgent = userAgent;
            UserBusiness target = GetTarget(agentFactory);
            // Expect
            Expect.Call(userAgent.IsValidWebUser(_context, "username", "password")).Return(true);
            // Replay
            _mocks.ReplayAll();
            bool actual = target.IsValidWebUser("username", "password");
            Assert.AreEqual(true, actual);
            _mocks.VerifyAll();
        }

        /// <summary>
        ///A test for IsValidWebUser. Test ticketid
        ///</summary>
        [TestMethod()]
        public void IsUserNameUniqueTest()
        {
            var agentFactory = new MockAgentFactory();
            var userAgent = _mocks.StrictMock<IUserAgent>();
            agentFactory.UserAgent = userAgent;
            UserBusiness target = GetTarget(agentFactory);
            // Object
            const string username = "username";
            int? ticketId = 42;
            // Expect
            Expect.Call(userAgent.IsUserNameUnique(_context, username)).Return(true);
            // Replay
            _mocks.ReplayAll();
            bool actual = target.IsUserNameUnique(username);
            Assert.AreEqual(true, actual);
            _mocks.VerifyAll();
        }

        /// <summary>
        ///A test for IsValidWebUser. Ticketid == null
        ///</summary>
        [TestMethod()]
        public void IsUserNameUniqueTest2()
        {
            var agentFactory = new MockAgentFactory();
            var userAgent = _mocks.StrictMock<IUserAgent>();
            agentFactory.UserAgent = userAgent;
            UserBusiness target = GetTarget(agentFactory);
            // Object
            const string username = "username";
            int? ticketId = null;
            // Expect
            Expect.Call(userAgent.IsUserNameUnique(_context, username)).Return(false);
            // Replay
            _mocks.ReplayAll();
            bool actual = target.IsUserNameUnique(username);
            Assert.AreEqual(false, actual);
            _mocks.VerifyAll();
        }

        /// <summary>
        ///A test for IsValidWebUser
        ///</summary>
        [TestMethod()]
        public void IsValidWebUserTest2()
        {
            var agentFactory = new MockAgentFactory();
            var userAgent = _mocks.StrictMock<IUserAgent>();
            agentFactory.UserAgent = userAgent;
            UserBusiness target = GetTarget(agentFactory);
            // Expect
            Expect.Call(userAgent.IsValidWebUser(_context, "username", "password")).Return(false);
            // Replay
            _mocks.ReplayAll();
            bool actual = target.IsValidWebUser("username", "password");
            Assert.AreEqual(false, actual);
            _mocks.VerifyAll();
        }

        /// <summary>
        ///A test for GetUser
        ///</summary>
        [TestMethod()]
        public void GetUserTest()
        {
            var agentFactory = new MockAgentFactory();
            var userAgent = _mocks.StrictMock<IUserAgent>();
            agentFactory.UserAgent = userAgent;
            UserBusiness target = GetTarget(agentFactory);
            // Object
            User user = new User() { Username = "gurgel@bah.com"};
            // Expect
            Expect.Call(userAgent.GetUser(_context, user.Username)).Return(user);
            // Replay
            _mocks.ReplayAll();
            User actual = target.GetUser(user.Username);
            Assert.AreEqual(user, actual);
            _mocks.VerifyAll();
        }

        /// <summary>
        ///A test for UserBusiness Constructor
        ///</summary>
        [TestMethod()]
        public void UserBusinessConstructorTest()
        {
            const User user = null;
            UserBusiness target = new UserBusiness(user);
            Assert.IsNotNull(target);
        }
    }
}